package cn.tedu.hm.wep;

import cn.tedu.hm.ex.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 说明:
 *
 * @ author: WeiZhen
 * @date: 2023/2/22
 **/
@Data
@Accessors(chain = true)
public class JsonResult<T> implements Serializable {
    /*响应状态码*/
    @ApiModelProperty("业务状态码")
    private Integer state;
    /*操作失败的提示*/
    @ApiModelProperty("提示内容")
    private String message;
    /*操作成功的返回数据*/
    @ApiModelProperty("数据")
    private T data;

    public static JsonResult<Void> ok(){
        return ok(null);
    }

    public static <T>JsonResult<T> ok(T data) {
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.setState(ServiceCode.OK.getValue());
        jsonResult.setData(data);
        return jsonResult;
    }
    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getCode(),e.getMessage());
    }

    public static JsonResult<Void> fail(ServiceCode state,String message){
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.state=state.getValue();
        jsonResult.message=message;
        return jsonResult;
    }

}
